const Fork = require('../lib/worker');
const path = require('path');
const chalk = require('chalk');
const fs = require('fs').promises;
const isExist = require('../lib/helper/utils').isExist;
const util = require('./util');

module.exports = function (REPO_ROOT, flag) {
  const repoName = 'test2';
  const f2 = Fork.default();
  f2.on('message', async data => {
    if (data.message === 'EXIT') {
      const p = path.join(REPO_ROOT, repoName, 'index.txt');
      if (await isExist(p)) {
        const content = await fs.readFile(p, 'utf8');
        if (content === 'hi!') {
          console.log(repoName, util.clone(1), util.tag(1));
        } else if (content === 'hello!') {
          console.log(repoName, util.clone(1), util.tag(0));
        }
      } else {
        console.log(repoName, chalk.red('NO'));
      }
      f2.kill(9);
      flag.count++;
    }
  }).send({
    options: {
      httpUrl: 'https://gitee.com/satrong/test2.git',
    },
    REPO_NAME: repoName,
    TAG: 'v1.0.0',
    REPO_ROOT,
    ROOT: __dirname,
  });
}